'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class TextField
	Private objHtmlTextField
	Private strHtmlTextFieldUniqueId
	
	Public Sub Class_Initialize()
		Set objHtmlTextField = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objHtmlTextField = Nothing
	End Sub
	
	Private Function IsTextField(ByVal objHtmlElement)
		IsTextField = False
		
		If UCase(objHtmlElement.TagName) = "INPUT" Then
			If UCase(objHtmlElement.Type) = "TEXT" Or UCase(objHtmlElement.Type) = "PASSWORD" Then
				IsTextField = True
			End If
		End If
	End Function	
	
	Public Sub WrapElement(ByVal objHtmlElement)
		If Not (objHtmlElement Is Nothing) Then
			If IsTextField(objHtmlElement) Then
				strHtmlTextFieldUniqueId = objHtmlElement.UniqueId
			
				Set objHtmlTextField = objHtmlElement
				
				Exit Sub
			End If
		End If

		Err.Raise 10001, "TextField->WrapElement", "The Html Element couldn't be wrapped"
	End Sub
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False
		
		If Not IsTextField(objHtmlTextField) Then Exit Function
		
		strHtmlElementTagName = objHtmlTextField.TagName
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function
	
	Public Sub FireEvent(ByVal strEventName)
		If Exists() Then
			objHtmlTextField.FireEvent strEventName
		Else
			Err.Raise 10002, "TextField->FireEvent", "The Text Field doesn't exist"
		End If
	End Sub
	
	Public Function Element()
		Set Element = Nothing
		
		If Not Exists() Then
			Err.Raise 10002,  "TextField->Element", "The Text Field doesn't exist"
			
			Exit Function
		End If
		
		Set Element = objHtmlTextField
	End Function
	
	Public Function GetProperty(ByVal strPropertyName)
		If Exists() Then
			If Internal_GetProperty(objHtmlTextField, strPropertyName, varPropertyName) Then
				GetProperty = varPropertyName
			Else
				Err.Raise 10003, "TextField->GetProperty", "The Text Field doesn't support this property"
			End If
		Else
			Err.Raise 10002, "TextField->GetProperty", "The Text Field doesn't exist"
		End If
	End Function

	Public Sub SetValue(ByVal strValue)
		If Not Exists() Then
			Err.Raise 10002, "TextField->SetValue", "The Text Field doesn't exist"
			
			Exit Sub
		End If
		
		If GetProperty("disabled") Then
			Err.Raise 10004, "TextField->SetValue", "The Text Field is disabled"
			
			Exit Sub
		End If
		
		If GetProperty("read only") Then
			Err.Raise 10005, "TextField->SetValue", "The Text Field is read only"
			
			Exit Sub
		End If
		
		If GetProperty("max length") < Len(strValue) Then
			Err.Raise 10006, "TextField->SetValue", "The size of the value is much bigger than max length of the Text Field"
			
			Exit Sub
		End If
		
		Call FireEvent("onFocus")
		Call FireEvent("onClick")
		
		objHtmlTextField.Value = strValue
		
		Call FireEvent("onChange")
	End Sub
End Class